package game.roles.gamer;

import com.javaGame.util.MusicUtil;
import game.frame.MainFrame;
import game.frame.StartFrame;
import game.roles.Role;
import game.roles.bullets.DropTank;
import game.roles.bullets.EnemyBullet;
import game.roles.bullets.RoundBoom;
import game.roles.enemys.EnemyRole;
import game.roles.gifts.Gift1;
import game.roles.visual.BackgroundMaps;
import game.utils.ImageChange;
import game.utils.PressListenerOne;
import game.utils.PressListenerTwo;

import java.awt.*;
import java.util.Random;

/**
 * @author liuyulai
 * Created with IntelliJ IDEA.
 * Date: 21.5.10
 * Time: 15:53
 * Description: 友军直升机
 */
public class Helicopter extends BaseGamer {
    public static Image[] helicopterImages;

    public static Image[] helicopterImagesBack;

    private ImageChange rm = new ImageChange(3);

    public boolean dropOrNot = false;

    /**
     * 是否为无敌状态
     */
    public boolean isInvincible = true;

    /**
     * 状态
     */
    private int statues = 0;

    /**
     * 初始状态
     */
    public static final int GAMER2_INIT = 0;

    /**
     * 前进状态
     */
    public static final int GAMER2_FRONT = 1;

    /**
     * 后退状态
     */
    public static final int GAMER2_BACK = 2;

    /**
     * 旋转
     */
    public static final int GAMER2_SPIN = 3;

    /**
     * 死亡状态
     */
    public static final int GAMER2_DEAD = 4;

    private int count = 0;

    public static boolean allowFireByThree = false;


    public Helicopter(int x, int y) {
        super(x, y, 160, 160);
    }

    public Helicopter() {
        super(-120, 100, 160, 160);
    }

    @Override
    public void rolesMove() {
        //单人模式下为技能操作
        if (!StartFrame.isTwoPlayerOrNot) {
            this.img = rm.change(helicopterImages);
            this.y += 2;
            if (this.y >= 180 && !dropOrNot) {
                this.y = 180;
                if (!dropOrNot) {
                    MainFrame.roleList.add(new DropTank(this.x, this.y));
                    dropOrNot = true;
                }
            } else if (dropOrNot) {
                this.y -= 4;
            }
        } else {
            if (statues == GAMER2_INIT) {
                this.setInit();
            } else if (statues == GAMER2_SPIN) {
                this.setGamerMove();
            } else if (statues == GAMER2_DEAD) {
                this.setDead();
            } else {
                this.setGamerMove();
                this.setByHit();
            }
            this.setImage();
        }
    }

    /**
     * 控制第二个角色进行移动
     */
    @Override
    protected void setGamerMove() {
        if (isInvincible) {
            count++;
            if (count >= 40) {
                isInvincible = false;
                count = 0;
            }
        }
        if (PressListenerTwo.isRight2) {
            this.x += 10;
            if (this.x >= 1200 && BackgroundMaps.mapMoveOrNot) {
                this.x = 1200;
            }
            if (BackgroundMaps.mapMoveOrNot) {
                BackgroundMaps.mapX -= 2;
            }
            this.statues = GAMER2_FRONT;
        } else if (PressListenerTwo.isLeft2) {
            this.x -= 10;
            if (this.x <= 0) {
                this.x = 0;
            }
            this.statues = GAMER2_BACK;
        }
        if (PressListenerTwo.isUp2) {
            this.y -= 10;
            if (this.y <= 100) {
                this.y = 100;
            }
        } else if (PressListenerTwo.isDown2) {
            this.y += 10;
            if (this.y >= 250) {
                this.y = 250;
            }
        }
    }

    /**
     * 发射炸弹
     */
    public void gamerSendBoom() {
        if (MainFrame.boomNum <= 0) {
            return;
        }
        MainFrame.boomNum--;
        this.statues = GAMER2_SPIN;
        MainFrame.roleList.add(new DropTank(this.x, this.y));
    }

    /**
     * 设置图片显示状态
     */
    protected void setImage() {
        if (statues == GAMER2_INIT || statues == GAMER2_FRONT && isInvincible == true) {
            this.img = this.rm.change(helicopterImages);
        } else if (statues == GAMER2_FRONT && isInvincible == false) {
            this.img = this.rm.change(helicopterImages);
        } else if (statues == GAMER2_BACK && isInvincible == false) {
            this.img = this.rm.change(helicopterImagesBack);
        } else if (statues == GAMER2_BACK && isInvincible == true) {
            this.img = this.rm.change(helicopterImagesBack);
        } else if (statues == GAMER2_SPIN) {
            this.img = this.rm.change(helicopterImages);
        } else if (statues == GAMER2_DEAD) {
            this.img = this.rm.change(gamerDead);
        }
    }


    /**
     * 设置死亡状态
     */
    @Override
    protected void setDead() {
        this.y -= 30;
        //让玩家掉出场地
        System.out.println(this.y);
        if (this.y <= -200) {
            MainFrame.roleList.remove(this);
            MainFrame.gamerNum--;
            if (MainFrame.gamerNum < 0) {
                MainFrame.isPass = false;
                MainFrame.flag = false;
            } else {
                Helicopter helicopter = new Helicopter();
                MainFrame.roleList.add(MainFrame.helicopter = helicopter);
            }
        }
    }

    /**
     * 设置初始状态
     */
    @Override
    protected void setInit() {
        this.x += 5;
        if (this.x >= 100) {
            statues = GAMER2_FRONT;
        }
    }

    /**
     * 受击方法(此受击方法包括了被礼物击中)
     */
    @Override
    protected void setByHit() {
        for (int i = 0; i < MainFrame.roleList.size(); i++) {
            Role r = MainFrame.roleList.get(i);
            if (!isInvincible && this.rec.intersects(r.rec) && (r instanceof EnemyRole || r instanceof EnemyBullet)) {
                this.statues = GAMER2_DEAD;
                this.setDead();
                return;
            } else if (r instanceof Gift1 && this.rec.intersects(r.rec)) {
                new MusicUtil("music/getGift.mp3", false);
                gamerHitByGift();
                MainFrame.roleList.remove(r);
            }
        }
    }

    /**
     * 礼物效果
     */
    @Override
    protected void gamerHitByGift() {
        //生成0-6的随机数
        int random = new Random().nextInt(6);

        switch (random) {
            //炸弹数量+0到3个
            case 0:
                MainFrame.boomNum += new Random().nextInt(3);
                break;
            //允许玩家用三行子弹进行射击
            case 1:
                allowFireByThree = true;
                break;
            //玩家生命数量+1到5
            case 2:
                MainFrame.gamerNum += new Random().nextInt(5) + 1;
                break;
            default:
                MainFrame.playerExp += 2000;
        }
    }

    public int getStatues() {
        return statues;
    }

    public void setStatues(int statues) {
        this.statues = statues;
    }
}
